//
//  DISLoginViewController.m
//  Distributor
//
//  Created by 主事丫环 on 2019/5/5.
//  Copyright © 2019 主事丫环. All rights reserved.
//

#import "DISLoginViewController.h"
#import "CountBtn.h"
#import "CSaddPerCardViewController.h"
#import "CSIdAuthViewController.h"
#import "TabBarViewController.h"
#import "BBuildingShopViewController.h"
#import "JPUSHService.h"
#import "DPSendCodeButton.h"
#import "BShopNoIncomeVC.h"//B级经销商 无收益

@interface DISLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *phoneTF;
@property (weak, nonatomic) IBOutlet UITextField *yzmTF;
@property (weak, nonatomic) IBOutlet YZMButton *yzmButton;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topDistance;

@end

@implementation DISLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //背景图距离顶部的距离
    self.topDistance.constant = 44 - kNaviHeight;
    self.phoneTF.keyboardType = UIKeyboardTypeNumberPad;
    self.yzmTF.keyboardType = UIKeyboardTypeNumberPad;
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:YES];
}

#pragma mark - 获取验证码
- (IBAction)btnGetCodeClickAction:(YZMButton *)sender {
    
    //去除字符串中空格
    _phoneTF.text = [_phoneTF.text stringByReplacingOccurrencesOfString:@" " withString:@""];
    if (![self isMobileNumberOnly:_phoneTF.text]) {
        ShowToastWithText(@"请输入正确的手机号");
        return;
    }
    __weak typeof(self) weakself = self;
    NSDictionary *dic = @{@"phone":_phoneTF.text};
    
    [NetWorkTool POST:sendMsg param:[dic mutableCopy] success:^(id dic) {
        ShowToastWithText(@"验证码发送成功");
        [weakself.yzmButton timeFailBeginFrom:60];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:NO];
}

#pragma mark - 登录点击事件
- (IBAction)loginBUt:(UIButton *)sender {
    if (strIsEmpty(_yzmTF.text)) {
        ShowToastWithText(@"请输入验证码");
        return;
    }
    NSMutableDictionary *param = [NSMutableDictionary dictionaryWithCapacity:1];
    param[@"phone"] = _phoneTF.text;
    param[@"yzCode"] = _yzmTF.text;
    
    [NetWorkTool POST:chcLoginApi param:param success:^(id dic) {
        KMyLog(@"fanhuisjhujku%@",dic);
        
        NSString *toStr = [[[dic objectForKey:@"data"]  objectForKey:@"token"] objectForKey:@"token"];
        [USER_DEFAULT setObject:toStr forKey:@"Token"];
        NSString *level = [[[dic objectForKey:@"data"]  objectForKey:@"user"] objectForKey:@"level"];
        [USER_DEFAULT setObject:level forKey:@"level"];
        
        NSString *userid = [[[dic objectForKey:@"data"]  objectForKey:@"user"] objectForKey:@"id"];
        [USER_DEFAULT setObject:userid forKey:@"user_id"];
        
        NSString *id_card = [NSString stringWithFormat:@"%@",[[[dic objectForKey:@"data"] objectForKey:@"user"] objectForKey:@"id_card"]];
        NSString *shopid = [NSString stringWithFormat:@"%@",[[[dic objectForKey:@"data"] objectForKey:@"user"] objectForKey:@"shopid"]];
        
        NSString *invitation_code = [NSString stringWithFormat:@"%@",[[[dic objectForKey:@"data"] objectForKey:@"user"] objectForKey:@"invitation_code"]];
        
        [USER_DEFAULT setObject:invitation_code forKey:@"invitation_code"];
        
        [JPUSHService setAlias:KUID completion:^(NSInteger iResCode, NSString *iAlias, NSInteger seq) {
            
        } seq:1];
        
        NSLog(@"%@",KUID);
        //市县级 未缴费
        if (AGENT_LEVEL !=1)
        {
            [self requestcountmy];
        }
        if (strIsEmpty(id_card)) {
            //没进行身份证认证 去认证
            CSIdAuthViewController *card = [[CSIdAuthViewController alloc]init];
            card.isstr = shopid;
            [self.navigationController pushViewController:card animated:YES];
            return ;
        };
        
        if ([level integerValue] == 1) {
            //展示A级经销商
            TabBarViewController *tabbar = [[TabBarViewController alloc]init];
            [UIApplication sharedApplication].keyWindow.rootViewController = tabbar;
        } else if ([level integerValue] == 2){
            if(strIsEmpty(shopid)){
                //现在需求 不再进行认证店铺
                TabBarViewController *tabbar = [[TabBarViewController alloc]init];
                [UIApplication sharedApplication].keyWindow.rootViewController = tabbar;
            } else {
                //展示b有收益
                TabBarViewController *tabbar = [[TabBarViewController alloc]init];
                [UIApplication sharedApplication].keyWindow.rootViewController = tabbar;
            }
            
        }else if([level integerValue] == 3){
            if(strIsEmpty(shopid)){
                //去认证店铺
                [self.navigationController pushViewController:[BBuildingShopViewController new] animated:YES];
            }else{
                //展示b没收益(暂时不考虑)
                //BShopNoIncomeVC *vc = [BShopNoIncomeVC new];
                //BaseNavViewController *nav = [[BaseNavViewController alloc] initWithRootViewController:vc];
                //[UIApplication sharedApplication].keyWindow.rootViewController = nav;
            }
        }
        //level = 1  A级 2 B级有收益 3 B级没收益 -> id_card 没有数据就去身份证
        //请求用户的收获地址
        //[self checkUserAddressList];
    } other:^(id dic) {
        ShowToastWithText(dic[@"msg"]);
    } fail:^(NSError *error) {
        
    } needUser:NO];
}

//市县级 是否缴费查询
-(void)requestcountmy{
    
    [NetWorkTool POST:CcentenInfo param:nil success:^(id dic) {
        NSString *string = [[dic objectForKey:@"data"] objectForKey:@"is_power"];
        
        if (string.length>0  && [string integerValue]== 0 ) {
            [USER_DEFAULT setObject:string forKey:@"is_power"];
        }
        else if (string.length>0 && [string integerValue]== 1 ) {
            [USER_DEFAULT setObject:string forKey:@"is_power"];
        }
        
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

///判断是否为电话号码
- (BOOL)isMobileNumberOnly:(NSString *)mobileNum {
    
    NSString * MOBILE = @"^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\\d{8}$";
    NSPredicate *regextestmobile = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", MOBILE];
    if ([regextestmobile evaluateWithObject:mobileNum] == YES) {
        return YES;
    } else {
        return NO;
    }
}

#pragma mark - 请求经销商地址列表
- (void)checkUserAddressList{
    [NetWorkTool POST:checkAddressList param:nil success:^(id dic) {
        
    } other:^(id dic) {
        
    } fail:^(NSError *error) {
        
    } needUser:YES];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
